package com.wengu.algorithm.service.impl;

import com.wengu.algorithm.dto.TaskAlg;
import com.wengu.algorithm.dto.TaskAlgResult;
import com.wengu.algorithm.dto.UserAlg;
import com.wengu.algorithm.entity.AssignmentOutcome;
import com.wengu.algorithm.entity.AssignmentOutcomeUser;
import com.wengu.algorithm.mapper.AssignmentOutcomeUserMapper;
import com.wengu.algorithm.service.AssignmentOutcomeUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 单个用户模拟任务分配结果详情 服务实现类
 * </p>
 *
 * @author wanghailei
 * @since 2025-03-02
 */
@Slf4j
@Service
public class AssignmentOutcomeUserServiceImpl extends ServiceImpl<AssignmentOutcomeUserMapper, AssignmentOutcomeUser> implements AssignmentOutcomeUserService {

    @Override
    public void saveOutcomeUser(UserAlg user, AssignmentOutcome assignmentOutcome, List<TaskAlg> selected, double totalDeltaF) {
        AssignmentOutcomeUser assignmentOutcomeUser = new AssignmentOutcomeUser();
        assignmentOutcomeUser.setOutcomeId(assignmentOutcome.getId());
        assignmentOutcomeUser.setMainId(assignmentOutcome.getMainId());
        assignmentOutcomeUser.setUserId(user.getId());
        assignmentOutcomeUser.setVersionNo(assignmentOutcome.getVersionNo());
        assignmentOutcomeUser.setCreateDate(new Date());
        assignmentOutcomeUser.setUpdateDate(new Date());
        assignmentOutcomeUser.setDeleteFlag(0);
        assignmentOutcomeUser.setTaskNumber(selected.size());
        List<TaskAlgResult> taskIdList = selected.stream().map(t -> {
            TaskAlgResult result = new TaskAlgResult();
            result.setId(t.getId());
            result.setK(t.k);
            return result;
        }).collect(Collectors.toList());
        assignmentOutcomeUser.setTaskList(taskIdList.toString());
        assignmentOutcomeUser.setDistanceCost(user.getDistanceCost());
        assignmentOutcomeUser.setExecutionCost(user.getExecutionCost());
        assignmentOutcomeUser.setTotalCost(user.getDistanceCost()+ user.getExecutionCost());
        assignmentOutcomeUser.setBudget(user.getBudget());
        assignmentOutcomeUser.setTotalQuality(totalDeltaF);
        assignmentOutcomeUser.setAverageQuality(0d);
        if (!selected.isEmpty()){
            assignmentOutcomeUser.setAverageQuality(totalDeltaF/selected.size());
        }
        this.save(assignmentOutcomeUser);
    }

    @Override
    public void saveOutcomeUser(UserAlg user, AssignmentOutcome assignmentOutcome) {
        AssignmentOutcomeUser assignmentOutcomeUser = new AssignmentOutcomeUser();
        assignmentOutcomeUser.setOutcomeId(assignmentOutcome.getId());
        assignmentOutcomeUser.setMainId(assignmentOutcome.getMainId());
        assignmentOutcomeUser.setUserId(user.getId());
        assignmentOutcomeUser.setVersionNo(assignmentOutcome.getVersionNo());
        assignmentOutcomeUser.setCreateDate(new Date());
        assignmentOutcomeUser.setUpdateDate(new Date());
        assignmentOutcomeUser.setDeleteFlag(0);
        assignmentOutcomeUser.setTaskNumber(user.realPath.size());
        List<TaskAlgResult> taskIdList = user.realPath.stream().map(t -> {
            TaskAlgResult result = new TaskAlgResult();
            result.setId(t.getId());
            result.setK(t.k);
            return result;
        }).collect(Collectors.toList());
        assignmentOutcomeUser.setTaskList(taskIdList.toString());
        List<TaskAlgResult> taskListB = user.pathB.stream().map(t -> {
            TaskAlgResult result = new TaskAlgResult();
            result.setId(t.getId());
            result.setK(t.k);
            return result;
        }).collect(Collectors.toList());
        assignmentOutcomeUser.setTaskListB(taskListB.toString());
        List<TaskAlgResult> taskListETDA = user.pathETDA.stream().map(t -> {
            TaskAlgResult result = new TaskAlgResult();
            result.setId(t.getId());
            result.setK(t.k);
            return result;
        }).collect(Collectors.toList());
        assignmentOutcomeUser.setTaskListEtda(taskListETDA.toString());
        assignmentOutcomeUser.setDistanceCost(user.getDistanceCost());
        assignmentOutcomeUser.setExecutionCost(user.getExecutionCost());
        assignmentOutcomeUser.setTotalCost(user.getDistanceCost()+ user.getExecutionCost());
        assignmentOutcomeUser.setBudget(user.getBudget());
        assignmentOutcomeUser.setTotalQuality(user.realDeltaF);
        assignmentOutcomeUser.setTotalQualityB(user.deltaFB);
        assignmentOutcomeUser.setTotalQualityEtda(user.deltaFETDA);
        assignmentOutcomeUser.setAlgName(user.algName);
        assignmentOutcomeUser.setAverageQuality(0d);
        if (!user.realPath.isEmpty()){
            assignmentOutcomeUser.setAverageQuality(user.realDeltaF/user.realPath.size());
        }
        this.save(assignmentOutcomeUser);
    }
}
